package com.tcl.plugin;

import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;
import org.objectweb.asm.Opcodes;

import static org.objectweb.asm.Opcodes.AASTORE;
import static org.objectweb.asm.Opcodes.ANEWARRAY;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.GETSTATIC;
import static org.objectweb.asm.Opcodes.ICONST_0;
import static org.objectweb.asm.Opcodes.ICONST_1;
import static org.objectweb.asm.Opcodes.IFEQ;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.RETURN;

public class ClassAdapterVisitor extends ClassVisitor {

    public ClassAdapterVisitor(ClassVisitor classVisitor) {
        super(Opcodes.ASM5, classVisitor);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        System.out.println("方法："+name+"，签名："+desc);

        MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
//        if("a".equals(name) && "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ldji/ux/internal/SlidingDialog$OnEventListener;)Ldji/ux/internal/SlidingDialog;"
//                .equals(desc)){
//
//        }
//        return  mv;
//        return new MethodAdapterVisitor(api,mv,access,name, desc);
        return new MethodAdapterVisitor(mv);
    }

    static class MethodAdapterVisitor extends MethodVisitor {

        private Object mValue;
        /*protected MethodAdapterVisitor(int api,MethodVisitor methodVisitor, int access, String name, String descriptor) {
            super(api, methodVisitor, access, name, descriptor);
        }*/


        public MethodAdapterVisitor(MethodVisitor mv) {
            super(Opcodes.ASM5, mv);
        }


        @Override
        public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
            super.visitLocalVariable(name, descriptor, signature, start, end, index);
            System.out.println("visitLocalVariable->"+name+"--"+descriptor+"--"+signature);
        }

        @Override
        public void visitLdcInsn(Object value) {
            super.visitLdcInsn(value);
            System.out.println("visitLdcInsn:"+value);
            mValue = value;
        }

        @Override
        public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
            super.visitFieldInsn(opcode, owner, name, descriptor);
            System.out.println("visitFieldInsn->"+owner+",name:"+name+",descriptor:"+descriptor);
        }

        @Override
        public void visitParameter(String name, int access) {
            super.visitParameter(name, access);
            System.out.println("visitParameter->"+name+",access:"+access);
        }



        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
//        super.visitMethodInsn(opcode, owner, name, desc, itf);
//            System.out.println("调用类名:"+owner+",方法："+name+"，2签名："+desc);
            if("com/tcl/utilcode/util/LogUtils".equals(owner) && "e".equals(name)){
                mv.visitFieldInsn(GETSTATIC, "com/tcl/utilcode/LogConfig", "LOGD_ENABLED", "Z");
                Label l1 = new Label();
                mv.visitJumpInsn(IFEQ, l1);
//                Label l2 = new Label();
//                mv.visitLabel(l2);
//                mv.visitLineNumber(24, l2);
//                mv.visitInsn(ICONST_1);
//                mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
//                mv.visitInsn(DUP);
//                mv.visitInsn(ICONST_0);
//                mv.visitLdcInsn(mValue);
//                mv.visitInsn(AASTORE);
//                mv.visitMethodInsn(INVOKESTATIC, "com/tcl/utilcode/util/LogUtils", "e", "([Ljava/lang/Object;)V", false);
                super.visitMethodInsn(opcode, owner, name, desc, itf);
                mv.visitLabel(l1);
//                mv.visitLineNumber(31, l1);
//                mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
//                mv.visitInsn(RETURN);
//                mv.visitMaxs(4, 0);
//                mv.visitEnd();
            }else
                super.visitMethodInsn(opcode, owner, name, desc, itf);
        }
    }
}
